package cn.justforchris.system.controller;




import cn.justforchris.system.annotation.PreAuthorize;
import cn.justforchris.system.domain.Employee;
import cn.justforchris.system.query.EmployeeQuery;
import cn.justforchris.system.service.IEmployeeService;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户接口
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "用户接口")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 新增
     * @param
     * @return
     */
    @PutMapping
    @PreAuthorize(name="保存职员",sn="employee:save")
    public JsonResult insertOrUpdate(@RequestBody Employee employee) {
       try {
           if (employee.getId() == null) {
            employeeService.insertEmployee(employee);
        } else {
            employeeService.updateEmployee(employee);
        }
        return JsonResult.success();
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.error();
       }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deletEmployee(@PathVariable Long id){
        try{
            employeeService.deleteEmployee(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelet(@RequestBody Long[] ids){
        try{
            employeeService.patchDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee getEmployee(@PathVariable Long id){

        return employeeService.getEmployeeById(id);

    }

    /**
     * 获取所有
     * @return
     */
    @GetMapping
    public List<Employee> getAll(){
        return employeeService.getAll();
    }

    /**
     * 分页查询
     * @param queryPage
     * @return
     */
    @PostMapping
    public PageInfo<Employee> queryPage(@RequestBody EmployeeQuery queryPage){
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return employeeService.queryPage(queryPage);

    }

    @PutMapping("/change")
    public JsonResult change(@RequestBody Employee employee){
        try {
            employeeService.change(employee);
            return JsonResult.success();
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }











}
